<template>
  <el-dialog title="非镜片类库存预警设置" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row :gutter="15">
        <el-col :span="8">
          <el-form-item label="制造商" prop="supplyName">
            <el-input v-model="dataForm.supplyName" placeholder="制造商" readonly @focus="chooseSupply"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="所属品种" prop="brandName">
            <el-input v-model="dataForm.brandName" placeholder="所属品种" readonly @focus="chooseBrand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="仓位" prop="redAlert">
            <el-select v-model="dataForm.stockId" placeholder="请选择" @change="stockSelect($event)">
              <el-option v-for="item in stockOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :span="8">
          <el-form-item label="库存上限" prop="inventoryTop">
            <el-input v-model="dataForm.inventoryTop" placeholder="库存上限"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="库存下限" prop="inventoryFloor">
            <el-input v-model="dataForm.inventoryFloor" placeholder="库存下限"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="红色预警" prop="redAlert">
            <el-input v-model="dataForm.redAlert" placeholder="红色预警"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>

    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
    <search-brand v-if="brandVisible" ref="brandDialog" @refreshData="getBrandData"></search-brand>
  </el-dialog>
</template>

<script>
  import {
    getSupplyDataList,
    getGoodsBrandDataList,
    getStockDataList,
    warningSettingSaveNoLens
  } from '@/api/eims/warning-setting'
  import SearchSupply from '@/components/business/SearchSupply'
  import SearchBrand from '@/components/business/SearchBrand'
  export default {
    data() {
      return {
        ischeck: false,
        visible: false, // 最外层弹窗
        dataForm: {
          inventoryTop: '',
          inventoryFloor: '',
          redAlert: ''
        },
        supplyVisible: false, // 制造商弹窗
        supplyDataListLoading: false, // 制造商表格加载图标
        supplyDataList: [], // 制造商表格数据
        brandVisible: false, // 品种弹窗
        brandDataListLoading: false, // 品种表格加载图表
        brandDataList: [], // 品种表格数据
        stockOptions: [], // 仓位数据
        dataRule: {
          inventoryTop: [{
            required: true,
            message: '库存上限不能为空',
            trigger: 'blur'
          }],
          inventoryFloor: [{
            required: true,
            message: '库存下限不能为空',
            trigger: 'blur'
          }],
          redAlert: [{
            required: true,
            message: '红色预警不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    components: {
      SearchSupply,
      SearchBrand
    },
    methods: {
      init(id) {
        this.visible = true
        this.$nextTick(() => {
          getStockDataList().then(response => {
            if(response.data.code == '0'){
              let dataList = response.data.data.records
              let options = []
              dataList.forEach(item => {
                options.push({
                  value: item.id,
                  label: item.name
                })
              })
              this.stockOptions = options
            }else{
              this.$message.error(response.data.msg)
            }
          })
        })
      },
      // 查询供应商列表
      chooseSupply() {
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplyDialog.init({
            ctgId: ''
          })
        })
      },
      // 查询品种列表
      chooseBrand() {
        if (this.dataForm.supplyId) {
          this.brandVisible = true
          this.$nextTick(() => {
            this.$refs.brandDialog.init({
              ctgId: '',
              supplyId: this.dataForm.supplyId
            })
          })
        } else {
          this.$message.warning('请选择所属制造商')
        }
      },
      getSupplyData(data) {
        this.$set(this.dataForm, 'supplyId', data.id)
        this.$set(this.dataForm, 'supplyName', data.name)
      },
      getBrandData(data) {
        this.$set(this.dataForm, 'brandId', data.id)
        this.$set(this.dataForm, 'brandName', data.name)
      },
      stockSelect($event) {
        this.stockOptions.forEach(item => {
          if (item.value == $event) {
            this.dataForm.stockName = item.label
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.inventoryFloor > this.inventoryTop) {
              this.ischeck = false
              this.$message.warning('库存量下限不能大于库存量上限')
              return
            }
            if (this.redAlert > this.inventoryFloor) {
              this.ischeck = false
              this.$message.warning('库存量红色预警不能大于库存量下限')
              return
            }
            warningSettingSaveNoLens(this.dataForm).then(response => {
              if(response.data.code == '0'){
                this.$message.success('设置成功')
                this.visible = false
                this.$emit('inopticDataSetting')
              }else{
                this.$message.error(response.data.msg)
              }
              this.ischeck = false
            }).catch(() => {
              this.ischeck = false
            })
          } else {
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
